﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using System.Web.Mvc.Html;
using System.Text;
using System.IO;

using ComLib.Extensions;
using ComLib.Web.Lib.Core;


namespace ComLib.Web.Lib.Extensions
{
    /// <summary>
    /// Class to provided localized resource strings for labels.
    /// This is currently in place but using default data for now.
    /// </summary>
    public static class HtmlHelperExtensionsForAjax
    {
        /// <summary>
        /// Creates an anchor action link that makes an ajax call to the url supplied and alerts (via alert box) the result.
        /// </summary>
        /// <param name="htmlhelper">The HtmlHelper</param>
        /// <param name="linkText">The text to display for the link</param>
        /// <param name="actionUrl">The url of the ajax call</param>
        /// <param name="id">Optional id for the anchor.</param>
        /// <returns></returns>
        public static string AjaxLinkWithAlert(this HtmlHelper htmlhelper, string linkText, string actionUrl, string id = "")
        {
            if (!string.IsNullOrEmpty(id))
                id = @"id=""" + id + @"""";

            System.Web.HttpUtility.UrlEncode(actionUrl);
            var code = "AjaxHelper.SendActionAndAlert('" + actionUrl + "');";
            var control = @"<a href=""#"" {0} onclick=""{1}; return false;"" class=""actionlink"" >{2}</a>";
            control = string.Format(control, id, code, linkText);
            return control;
        }


        /// <summary>
        /// Creates an anchor action link that makes an ajax call to the url supplied and alerts (via alert box) the result.
        /// </summary>
        /// <param name="htmlhelper">The HtmlHelper</param>
        /// <param name="linkText">The text to display for the link</param>
        /// <param name="actionUrl">The url of the ajax call</param>
        /// <param name="id">Optional id for the anchor.</param>
        /// <returns></returns>
        public static string AjaxLinkWithMessage(this HtmlHelper htmlhelper, string linkText, string actionUrl, string divIdForMessage, string id = "")
        {
            if (!string.IsNullOrEmpty(id))
                id = @"id=""" + id + @"""";

            System.Web.HttpUtility.UrlEncode(actionUrl);
            var code = "AjaxHelper.SendActionAndDisplayMessage('" + actionUrl + "', '" + divIdForMessage + "');";
            var control = @"<a href=""#"" {0} onclick=""{1}; return false;"" class=""actionlink"" >{2}</a>";
            control = string.Format(control, id, code, linkText);
            return control;
        }


        /// <summary>
        /// Creates an anchor action link that makes an ajax call to the url supplied and alerts (via alert box) the result.
        /// </summary>
        /// <param name="htmlhelper">The HtmlHelper</param>
        /// <param name="linkText">The text to display for the link</param>
        /// <param name="actionUrl">The url of the ajax call</param>
        /// <param name="id">Optional id for the anchor.</param>
        /// <returns></returns>
        public static string AjaxLinkWithCallback(this HtmlHelper htmlhelper, string linkText, string actionUrl, string callback, string id = "")
        {
            if (!string.IsNullOrEmpty(id))
                id = @"id=""" + id + @"""";

            System.Web.HttpUtility.UrlEncode(actionUrl);
            var code = "AjaxHelper.SendActionAndCallback('" + actionUrl + "', " + callback + ");";
            var control = @"<a href=""#"" {0} onclick=""{1}; return false;"" class=""actionlink"" >{2}</a>";
            control = string.Format(control, id, code, linkText);
            return control;
        }
    }
}
